package com.le.tester.book.concurrentjavafight.l_14;

/**
 * createTime：2024/5/14 14:15
 * description：使用简单的阻塞实现有界缓存
 */
public class SleepBoundBuffer<V> extends BaseBoundedBuffer<V> {
    public SleepBoundBuffer(int capacity) {
        super(capacity);
    }

    public void put(V v) throws InterruptedException {

        while (true) {
            synchronized (this) {
                if (!isFull()) {
                    doPut(v);
                    return;
                }
            }
            Thread.sleep(10);
        }
    }


    public V take() throws InterruptedException {
        while (true) {
            synchronized (this) {
                if (!isEmpty()) {
                    return doTake();
                }
            }
            Thread.sleep(10);
        }
    }
}
